package com.aq.sales.business.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aq.sales.business.intf.LookupBO;
import com.aq.sales.entity.Lookup;
@Service
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class LookupBOImpl extends CommonBOImpl<Lookup> implements
		LookupBO<Lookup> {

	@Override
	public Lookup findById(int id) {
		return  (Lookup)getSessionFactory().openSession().load(com.aq.sales.entity.Lookup.class ,new Integer(id));
	}
	
	@Override
	public Lookup findById(int id,int accId) {
		Query qry=getCurrentSession().createQuery("from Lookup where id=:id");
		qry.setInteger("id", id);
		return (Lookup)qry.uniqueResult();
	}
	
	@Override
	public List<Lookup> findAll(int accId) {
		Query qry=getSessionFactory().getCurrentSession().createQuery("from Lookup where accId=:accId");
		qry.setInteger("accId", accId);
		return qry.list();
	}
	

	@Override
	public int insert(Lookup obj) {
		getSessionFactory().getCurrentSession().save(obj);
		return 0;
	}

	@Override
	@Transactional
	public int update(Lookup obj) {
		getSessionFactory().getCurrentSession().saveOrUpdate(obj);
		return 0;
	}

	@Override
	public int delete(Lookup obj) {
		getSessionFactory().getCurrentSession().delete(obj);
		return 0;
	}

	@Override
	public List<Lookup> getPropertiesByKey(String key,int accId) {
		Query qry=getSessionFactory().getCurrentSession().createQuery("from Lookup where name=:name AND accId=:accId");
		qry.setString("name", key);
		qry.setInteger("accId", accId);
		return qry.list();
	}
	@Override
	public List<String> getPropertiesByKeyAsList(String key,int accId) {
		List<Lookup> lk=getPropertiesByKey(key,accId);
		List<String> lst=new ArrayList<String>();
		for(Lookup ob:lk){
			lst.add(ob.getDescription());
		}
		return lst;
	}

	@Override
	public List<Lookup> query(String where,int accId) {
		String sql="from Lookup where "+where+" AND accId=:accId";
		Query qry=getCurrentSession().createQuery(sql);
		qry.setInteger("accId", accId);
		return qry.list();
	}
}
